using Digitalmes.Application.Mes.Equipments.Inspections;
using Digitalmes.Domain.Entities.Mes.Equipments;

namespace Digitalmes.Application.Mes.Equipments.InspectManuals.Queries;

/// <summary>
/// 手动点检计划查询命令。
/// </summary>
/// <param name="MachineryId">设备 Id</param>
public sealed record GetManualInspectionPlanQuery(long MachineryId) : IRequest<IResult<EquipInspectionPlanDto?>>;

internal sealed class GetManualInspectionListQueryHandler(IRepository<EquipInspectionPlan> repo) : IRequestHandler<GetManualInspectionPlanQuery, IResult<EquipInspectionPlanDto?>>
{
    public async Task<IResult<EquipInspectionPlanDto?>> Handle(GetManualInspectionPlanQuery request, CancellationToken cancellationToken)
    {
        // 根据点检计划下的手动点检项（同一种设备同一种点检类型只有一个）
        var plan = await repo.AsQueryable()
            .Includes(m => m.ManualInspectionItems)
            .Where(m => m.MachineryId == request.MachineryId && m.InspectType == InspectTypeEnum.Manual)
            .FirstOrDefaultAsync(cancellationToken);
        return Result.From(plan.Adapt<EquipInspectionPlanDto?>());
    }
}
